package Registration;

import java.io.RandomAccessFile;
import java.sql.ResultSet;
import User.Uservalidation;

import control.Control;
import dbmanager.DBManager;

public class RegisterRate 
{
		String Path;
		DBManager db = new DBManager();
		
		public RegisterRate(String Path) 
		{		
			this.Path = Path;
			db.getConnect();
		}
		public int Register()
		{
			int Count=0;
			
			try 
			{
				RandomAccessFile raf = new RandomAccessFile(Path,"rws");
				RandomAccessFile log = new RandomAccessFile("//home//server//Grocessary//Request.txt","rws");
				RandomAccessFile Error = new RandomAccessFile("//home//server//Grocessary//Error.txt","rws");
				
				String line = null;
				line=raf.readLine();
				String[] records = line.split("\t");
				String col[] = new String[8]; 
				col[0] = records[0];	//Item
				col[1] = records[1];	//Unit
				col[2] = records[2];	//Usergroup
				col[3] = records[3];	//Buy
				col[4] = records[4];	//Sell
				col[5] = records[5];	//MRP
				col[6] = records[6];     //Vat
				col[7] = records[7];	//Discount
				
				ResultSet rs = db.getSelect("SELECT * FROM command where fieldname='"+col[0]+"' and msgtype = 'rate_master'");
				rs.next();
				col[0] = rs.getString("spchar");
				
				rs = db.getSelect("SELECT * FROM command where fieldname='"+col[1]+"' and msgtype = 'rate_master'");
				rs.next();
				col[1] = rs.getString("spchar");
				
				rs = db.getSelect("SELECT * FROM command where fieldname='"+col[2]+"' and msgtype = 'rate_master'");
				rs.next();
				col[2] = rs.getString("spchar");
			
				rs = db.getSelect("SELECT * FROM command where fieldname='rate' and msgtype = 'rate_master'");
				rs.next();
				String rateChar = rs.getString("spchar");
				
				rs = db.getSelect("SELECT * FROM command where fieldname='buysell' and msgtype = 'rate_master'");
				rs.next();
				String buysellChar = rs.getString("spchar");
					
				String userid = "+911234567891";
				Uservalidation getpass = new Uservalidation(db);
				String password = getpass.getPasword(userid);
				
				while((line=raf.readLine())!=null)
				{
					String message ="";
					try 
					{
						String propString = "";
						records = line.split("\t");

						for (int i = 0; i < 3; i++) 
						{
							try 
							{
								String pv = records[i].trim();
								if (pv.length() == 0)
									pv = "0";
								propString = propString + col[i].trim()+ "\"" + pv + "\"";
							} catch (Exception e)
							{
								String pv = "0";
								propString = propString + col[i].trim()+ "\"" + pv + "\"";
							}
						}
					
						for (int j = 3; j < 8; j++) 
						{	
							String temp;
							try
							{
								String pv = records[j].trim();
								if (pv.length() != 0)
								{
									message = "11 rate_master add"+propString+rateChar+"\""+pv+"\""+buysellChar+"\""+col[j].trim()+"\""+"#"+password;
									Control controlClassObj = new Control(userid,message,db);
									controlClassObj.messageProcessing();
									temp = controlClassObj.requestProcess().toString();
									if(temp.contains("error"))
										Error.writeBytes(message+"\n");
							  }
							}
							catch (Exception e) {
								Error.writeBytes(message+"\n");
							}
							System.out.println("MESSGE IS :"+message);
						}						
						Count++;
					} 
					catch (Exception e) 
					{
						Error.writeBytes(message+"\n");
					}
				}
				log.close();
				Error.close();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			
			return Count;
		}
		public static void main(String[] args) 
		{
			RegisterRate reg = new RegisterRate("//home//server//Grocessary//grocessary.csv");
			System.out.println("RAte Counter :"+reg.Register());
			
			
		}
}